package io.github.talelin.latticy.model;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.github.talelin.autoconfigure.validator.DateTimeFormat;
import io.github.talelin.latticy.common.constant.Constant;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import io.github.talelin.latticy.util.DateTool;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 消息投递日志
 *
 * @author generator@wulongbo
 * @since 2022-07-22
 */
@Data
@Accessors(chain = true)
@TableName("msg_log")
@NoArgsConstructor
public class MsgLogDO {

    /**
     * 消息唯一标识
     */
    @TableId(value = "msg_id", type = IdType.INPUT)
    private Long msgId;

    /**
     * 消息体, json格式化
     */
    private String msg;

    /**
     * 交换机
     */
    private String exchange;

    /**
     * 路由键
     */
    private String routingKey;

    /**
     * 状态: 0投递中 1投递成功 2投递失败 3已消费
     */
    private Integer status;

    /**
     * 重试次数
     */
    private Integer tryCount;

    /**
     * 下一次重试时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    private Date nextTryTime;

    @JsonIgnore
    private Date createTime;

    @JsonIgnore
    private Date updateTime;

    public MsgLogDO(Long msgId, Object msg, String exchange, String routingKey) {
        this.msgId = msgId;
        this.msg = JSON.toJSONString(msg);
        this.exchange = exchange;
        this.routingKey = routingKey;

        this.status = Constant.MsgLogStatus.DELIVERING;
        this.tryCount = 0;
        Date date = new Date();
        this.createTime = date;
        this.updateTime = date;
        this.nextTryTime = (DateTool.plusMinutes(date, 1));
    }


}
